package cn.com.netedge.iot.frontend.datasave.job;

import cn.com.netedge.iot.frontend.datasave.config.DataSaveConfig;
import cn.com.netedge.iot.frontend.datasave.model.freeze.FreezeDataModel;
import com.alibaba.fastjson2.JSON;
import cn.com.netedge.iot.common.constants.CacheKeyConstants;
import cn.com.netedge.iot.common.constants.Globals;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018-09-07.
 */
@Component
public class DataCLoadSaveJob extends BaseSaveJob {

    @Override
    protected void init() {
        this.cacheKey = CacheKeyConstants.KEY_DATA_C_LOAD + DataSaveConfig.getMachineName();
        this.saveDataList = new ArrayList<FreezeDataModel>(Globals.CHUNK_RECORD_SIZE);
    }

    @Override
    protected FreezeDataModel convert(String item) {
        return JSON.parseObject(item, FreezeDataModel.class);
    }

    @Override
    protected void batchSaveData(List<?> saveDataList) {
        List<FreezeDataModel> list = (List<FreezeDataModel>) saveDataList;
        dataSaveService.saveFreezeLoadDataModel(list);
    }

    @Scheduled(fixedDelay = 15 * 1000) //15 s
    public void execute() {
        this.chunkRun();
    }
}
